var basePath;
$(function() {
  var location = (window.location + '').split('/');
  basePath = location[0] + '//' + location[2] + '/' + location[3];
});
var counts=0;
var type=0;
var markersArray=[];
var points=[];
var polygon;
var line;
var map = new qq.maps.Map(document.getElementById("container"),{
    // 地图的默认鼠标指针样式
    draggableCursor: "crosshair",
 
    // 拖动地图时的鼠标指针样式
    draggingCursor: "pointer"
});
var dots = new qq.maps.visualization.Dots({
	map: map, // 必填参数，指定显示散点图的地图对象
	style: {
		fillColor: "#3CF",
		strokeColor: "#00C",
		strokeWidth: 1,
		radius: 4
	}
});







//
$('#init_coors').click(function() {
	console.log('init coors');
	let data = [];
	$.ajax({
		url : basePath + "/init_coors.do",
		data : {
			
		},
		type : 'post',
		success : function(result) {
			console.log(result)
			for (var c=0;c<result.length;c++) {
				console.log(c);
				var coors = result[c].split('-');
				data.push({
					lat: parseFloat(coors[1]),
					lng: parseFloat(coors[0]),
					type:1
				});
			}
			dots.setData(data);
		}
	});
	return data;
});



var listener=qq.maps.event.addListener(
    map,
    'click',
    function(event) {
        counts=counts+1;
        var marker = new qq.maps.Marker({
            position: event.latLng,
            map: map,
            content:'点'+counts
        });
        var label=new qq.maps.Label({
            position: event.latLng,
            map: map,
            content:'点'+counts
        });
        markersArray.push(marker);
        markersArray.push(label);
        insetText("点"+counts+"_经度:"+event.latLng.getLng()+";纬度:"+event.latLng.getLat()+"-<br/>");
    }
);
 
document.getElementById("point").onclick=function () {
    if (type != 1) {
        reInit();
        type = 1;
    }
    qq.maps.event.removeListener(listener);
   listener=qq.maps.event.addListener(
        map,
        'click',
        function(event) {
            counts = counts + 1;
            addPoints(new qq.maps.LatLng(event.latLng.getLat(),event.latLng.getLng()));
        }
    );
};
document.getElementById("line").onclick=function () {
    if (type != 2) {
        reInit();
        type = 2;
    }
 
    qq.maps.event.removeListener(listener);
    listener=qq.maps.event.addListener(
        map,
        'click',
        function(event) {
            counts=counts+1;
            addPoints(new qq.maps.LatLng(event.latLng.getLat(),event.latLng.getLng()));
            points.push(new qq.maps.LatLng(event.latLng.getLat(),event.latLng.getLng()));
 
            if(counts>=2){
                if(counts>2){
                    line.setMap(null);
                }
                line = new qq.maps.Polyline({
                    path: points,
                    strokeColor: '#ffc64b',
                    strokeWeight: 5,
                    editable:false,
                    map: map
                });
            }
 
 
        }
    );
};
document.getElementById("rectangle").onclick=function () {
    if (type != 3) {
        reInit();
        type = 3;
    }
    qq.maps.event.removeListener(listener);
    listener=qq.maps.event.addListener(
        map,
        'click',
        function(event) {
            counts = counts + 1;
            addPoints(new qq.maps.LatLng(event.latLng.getLat(), event.latLng.getLng()));
            points.push(new qq.maps.LatLng(event.latLng.getLat(), event.latLng.getLng()));
            if (counts >=3) {
                if(counts>3){polygon.setMap(null)}
                 polygon = new qq.maps.Polygon({
                    path:points,
                    strokeColor: '#ffc64b',
                    strokeWeight: 5,
                    map: map
                });
            }
        }
    );
};
function addPoints(latLng) {
    var marker = new qq.maps.Marker({
        position: latLng,
        map: map,
        content:'点'+counts
    });
    var label=new qq.maps.Label({
        position: latLng,
        map: map,
        content:'点'+counts
    });
    markersArray.push(marker);
    markersArray.push(label);
    insetText("点"+counts+"_经度:"+latLng.getLng()+";纬度:"+latLng.getLat()+"-<br/>");
}
 
function reInit() {
    if (counts > 3 && type == 3) {
        polygon.setMap(null);
    }
    if (line) {
        line.setMap(null)
    }
    counts = 0;
    clearText();
    if (markersArray) {
        for ( var i in markersArray) {
            markersArray[i].setMap(null);
        }
    }
    points=[];
}
 
function insetText(str) {
    var result=document.getElementById("result");
    result.insertAdjacentHTML("beforeEnd",str);
}
function clearText() {
    var result=document.getElementById("result");
    result.innerHTML="";
}





$('#saveCoors').click(function() {
	console.log('click saveCoors');
	coors_text = $('#result').text();
	$.ajax({
		url : basePath + "/save_coors.do",
		data : {
			"coors_text" : coors_text
		},
		type : 'post',
		success : function(result) {
			alert("保存成功！");
			reInit();
		}
	});
});

$('#clearData').click(function() {
	console.log('clear data');
	reInit();
});
function initMapCoors(map) {
	
	// 创建散点图对象
	var dots = new qq.maps.visualization.Dots({
		map: map, // 必填参数，指定显示散点图的地图对象
		style: {
			fillColor: "#3CF",
			strokeColor: "#00C",
			strokeWidth: 1,
			radius: 10
		}
	});
	// 获取散点数据
	var data = [];
	data.push({
		lat:39.898148,
		lng:116.358948
	})
	
	// 向散点图传入数据
	dots.setData(data);

	

	
}


